package com.leslie.hhcommunity.controller.interceptor;

import com.leslie.hhcommunity.entity.User;
import com.leslie.hhcommunity.service.DataService;
import com.leslie.hhcommunity.util.HostHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author
 * @PackageName:com.leslie.hhcommunity.controller.interceptor
 * @ClassnName:DataInterceptor
 * @Description:
 * @Date 2022/1/12  15:44
 */
@Component
public class DataInterceptor implements HandlerInterceptor {

	@Autowired
	DataService dataService;
	@Autowired
	HostHolder hostHolder;

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
		//统计UV
		String ip = request.getRemoteHost();
		dataService.recordUV(ip);

		//统计DAU
		User user = hostHolder.getUser();
		if (user != null){
			dataService.recordDAU(user.getId());
		}
		return true;
	}
}
